package service

import (
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"shrimp-blog-server/app/dao"
	"shrimp-blog-server/app/model"
)

func (*ArticleService) AddArticle(req *ghttp.Request, article ...model.Article) {
	if len(article) == 0 {
		g.Log("添加文章信息").Info("文章信息为空")
		responses.ErrorResponseExit(req, "添加的文章信息为空")
	}
	affectRow, err := dao.Article.Insert(article...)
	if err != nil {
		g.Log("添加文章信息").Errorf("添加文章信息失败，err: %v", err)
		panic(gerror.Wrap(err, "添加文章信息失败"))
	}
	if affectRow == 0 {
		responses.ErrorResponseExit(req, "添加文章信息失败")
	}
	responses.SuccessResponse(req)
}

func (*ArticleService) UpdateArticle(req *ghttp.Request, articleRequestList ...model.ArticleRequest) {
	if len(articleRequestList) == 0 {
		g.Log("更新文章信息").Info("文章信息为空")
		responses.ErrorResponseExit(req, "更新的文章信息为空")
	}

	//TODO:更新关联关系
	//// 文章标签
	//var articleLabelList []model.ArticleLabel
	//for i := range articleRequestList {
	//	labelIdList := strings.Split(articleRequestList[i].Label, ",")
	//	for j := range labelIdList {
	//		articleLabelList = append(articleLabelList, model.ArticleLabel{
	//			ArticleId: articleRequestList[i].ID,
	//			LabelId:   gconv.Uint(labelIdList[j]),
	//			Status:    1,
	//		})
	//	}
	//}
	//// 文章分类
	//var articleSortList []model.ArticleSorts
	//for i := range articleRequestList {
	//	sortIdList := strings.Split(articleRequestList[i].Sorts, ",")
	//	for j := range sortIdList {
	//		articleSortList = append(articleSortList, model.ArticleSorts{
	//			ArticleId: gconv.Int(articleRequestList[i].ID),
	//			SortId:    gconv.Int(sortIdList[j]),
	//			Status:    1,
	//		})
	//	}
	//}
	//// 文章专题
	//var articleSubject = model.SubjectItem{
	//	SubjectId: gconv.Int(articleRequestList[0].Subject),
	//	ArticleId: gconv.Int(articleRequestList[0].ID),
	//	Status:    0,
	//}
	//// 先删除文章关联的专题

	// 更新文章信息
	var articleList []model.Article
	for i := range articleRequestList {
		articleList = append(articleList, articleRequestList[i].Convert())
	}
	affectRow, err := dao.Article.Update(articleList...)
	if err != nil {
		g.Log("更新文章信息").Errorf("更新文章信息失败，err: %v", err)
		panic(gerror.Wrap(err, "更新文章信息失败"))
	}
	if affectRow == 0 {
		responses.ErrorResponseExit(req, "更新文章信息失败")
	}
	responses.SuccessResponse(req)
}

func (*ArticleService) DeleteArticle(req *ghttp.Request, id ...uint) {
	if len(id) == 0 {
		g.Log("删除文章信息").Info("文章id为空")
		responses.ErrorResponseExit(req, "删除的文章id为空")
	}
	articleArr := make([]model.Article, len(id))
	for i, v := range id {
		articleArr[i].Id = v
		articleArr[i].Status = 1
	}

	affectRow, err := dao.Article.Delete(articleArr...)
	if err != nil {
		g.Log("删除文章信息").Errorf("删除文章信息失败，err: %v", err)
		panic(gerror.Wrap(err, "删除文章信息失败"))
	}
	if affectRow == 0 {
		responses.ErrorResponseExit(req, "删除文章信息失败")
	}
	responses.SuccessResponse(req)
}

func (*ArticleService) GetArticle(req *ghttp.Request, id ...uint) {
	if len(id) == 0 {
		responses.ErrorResponseExit(req, "文章id不能为空")
	}
	articleArr, err := dao.Article.GetModelById(id...)
	if err != nil {
		g.Log("获取文章信息").Errorf("获取文章信息失败，err: %v", err)
		panic(gerror.Wrap(err, "获取文章信息失败"))
	}
	if articleArr != nil {
		responses.ErrorResponseExit(req, "文章对应的信息不存在")
	}
	if len(articleArr) == 1 {
		responses.ErrorResponseExit(req, "文章对应的信息不存在")
		req.ExitAll()
	}

	responses.Json(req, successCode, successMessage, articleArr[0].Convert(articleArr))
}

func (*ArticleService) GetArticlePage(req *ghttp.Request, page model.Page) {
	articleArr, err := dao.Article.GetModelsByPage(page.Page, page.Size)
	if err != nil {
		g.Log("获取文章列表").Errorf("获取文章列表失败，err: %v", err)
		panic(gerror.Wrap(err, "获取文章列表失败"))
	}
	if articleArr == nil {
		responses.ErrorResponseExit(req, "文章列表为空")
	}
	articleResponses := articleArr[0].ConvertPage(articleArr, page)
	articleResponses.Page.Total, _ = dao.Article.DB().Model(dao.Article.Table).Count()
	if err != nil {
		panic(gerror.Wrap(err, "统计数量失败"))
	}
	responses.Json(req, successCode, successMessage, articleResponses)
}
